<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：后台首页控制器。
 *
 * */

namespace Qwadmin\Controller;

class IndexController extends ComController {

    public function index() {
        $model = new \Think\Model();
        $mysql = $model->query("select VERSION() as mysql");
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $t = time() - 3600 * 24 * 60;
        $log = M('log');
        $log->where("t < $t")->delete(); //删除60天前的日志
        $pagesize = 25; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $count = $log->count();
        $list = $log->order('id desc')->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('mysql', $mysql[0]['mysql']);
        $this->assign('nav', array('', '', '')); //导航
        //检测是否有添加仓库
        $this->assign('warehouse', ( M('customer_warehouse')->where(array('customer_id' => $this->__seller_info['id']))->find() ? 1 : 0));
        $this->assign('dispatching', ( M('dispatching')->where(array('customer_id' => $this->__seller_info['id']))->find() ? 1 : 0));
        $this->display();
    }

    /**
     * 上传大文件
     */
    public function upfileMax() {
        $fileName = $_FILES['myFile']['name'];
        $fileSize = $_FILES['myFile']['size'];
        if ($fileName != "") {
            if ($fileSize > 100 * 1024 * 1024) {
                echo '文件大小不能超过100M';
                exit;
            }
            $type = explode('.', $fileName);
            $ext = $type[count($type) - 1];
            $fileExt = array('zip', 'rar', 'pdf', 'doc', 'docx', 'xls', 'xlsx');
            if (in_array($fileExt, $ext)) {
                echo '文件格式不对，可上传的格式为：' . implode(',', $fileExt) . '！';
                exit;
            }
            $rand = rand(100, 999);
            $files = date("YmdHis") . $rand . '.' . $ext;
            //上传路径
            $file_path = SITE_FILE . "/Files/" . date('Y-m-d', time()) . '/';
            if (!file_exists($file_path)) {
                mkdir($file_path, 0777, true);
            }
            move_uploaded_file($_FILES['myFile']['tmp_name'], $file_path . $files);
        }
        $size = round($fileSize / 1024, 2);
        $arr = array(
            'name' => $fileName,
            'file' => '/Files/' . date('Y-m-d', time()) . '/' . $files,
            'size' => $size
        );
        echo json_encode($arr);
    }

}
